package com.xinchuang.service.targetSystem.Impl.damage;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinchuang.comment.R;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.entity.targetSystem.damage.AvoidHitting;
import com.xinchuang.entity.targetSystem.damage.DictionaryTable;
import com.xinchuang.mapper.targetSystem.damage.AvoidHittingMapper;
import com.xinchuang.service.targetSystem.damage.IAvoidHittingService;
import com.xinchuang.utils.CommonUtil;
import com.xinchuang.vo.AvoidHittingVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * BD区域管理 服务实现类
 * </p>
 *
 * @author zhb
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AvoidHittingServiceImpl extends ServiceImpl<AvoidHittingMapper, AvoidHitting> implements IAvoidHittingService {

    private final DictionaryTableServiceImpl dictionaryTableService;

    private final AvoidHittingMapper avoidHittingMapper;

    @Override
    public R selectEntities(int pageNum, int pageSize, String avoidHittingName, String avoidHittingType) {
        try {
            LambdaQueryWrapper<AvoidHitting> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(StringUtils.isNotBlank(avoidHittingName), AvoidHitting::getAvoidHittingName, avoidHittingName);
            wrapper.eq(StringUtils.isNotBlank(avoidHittingType), AvoidHitting::getAvoidHittingType, avoidHittingType);
            wrapper.eq(AvoidHitting::getDataStatus, 0);
            wrapper.orderByDesc(AvoidHitting::getCreateTime);
            Page<AvoidHitting> page = avoidHittingMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
            //重写定义分页插件返回参数
            Map<String, Object> map = new HashMap<>();
            if (CollectionUtil.isNotEmpty(page.getRecords())) {
                List<DictionaryTable> bdType = dictionaryTableService.getDictionaryTableList("bd_type");
                if (CollectionUtil.isNotEmpty(bdType)) {
                    page.getRecords().forEach(data -> {
                        for (DictionaryTable dictionaryTable : bdType) {
                            if (data.getAvoidHittingType().equals(dictionaryTable.getValue())) {
                                data.setAvoidHittingTypeLabel(dictionaryTable.getLabel());
                                break;
                            }
                        }
                    });
                }
            }
            map.put("dataList", page.getRecords());
            map.put("total", page.getTotal());
            return R.data(map, "获取成功");
        } catch (Exception e) {
            log.info("获取BD区域管理列表失败:{}", e.toString(), e);
        }
        return R.fail("获取失败");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addOrUpdateEntity(AvoidHittingVo form) {
        if (StringUtils.isBlank(form.getAvoidHittingName())) {
            return ResultUtil.error(500, "名称不能为空");
        }
        if (StringUtils.isBlank(form.getAvoidHittingType())) {
            return ResultUtil.error(500, "请选择类型");
        }
        if (!CommonUtil.isDouble(form.getLon()) || !CommonUtil.isDouble(form.getLat())) {
            return ResultUtil.error(500, "经度/纬度请输入点度格式的内容");
        }
        try {
            AvoidHitting h = new AvoidHitting();
            h.setId(form.getId());
            h.setAvoidHittingName(form.getAvoidHittingName());
            h.setAvoidHittingType(form.getAvoidHittingType());
            h.setLon(Double.parseDouble(form.getLon()));
            h.setLat(Double.parseDouble(form.getLat()));
            if (StringUtils.isNotBlank(form.getId())) {
                avoidHittingMapper.updateById(h);
                return ResultUtil.success(null, "修改成功");
            } else {
                form.setId(UUID.randomUUID().toString().replace("-", ""));
                avoidHittingMapper.insert(h);
                return ResultUtil.success(null, "新增成功");
            }
        } catch (Exception e) {
            log.info("增加或修改BD区域管理失败:{}", e.toString(), e);
        }
        return ResultUtil.error(500, "增加或修改失败");
    }


    @Override
    public R deltEntities(List<String> ids, boolean deleteStatus) {
        try {
            if (deleteStatus) {
                avoidHittingMapper.deleteBatchIds(ids);
            } else {
                String param = "'" + String.join("','", ids) + "'";
                avoidHittingMapper.deleteBatchIdsWithLogic(param);
            }
            return R.success("删除成功");
        } catch (Exception e) {
            log.info("删除BD区域管理失败:{}", e.toString(), e);
        }
        return R.fail("删除失败");
    }
}
